//
//  main.swift
//  swiftSort
//
//  Created by Bailinhan on 2017/9/13.
//  Copyright © 2017年 Bailinhan All rights reserved.
//

import Foundation


struct Student{
    var name:String
    var mathScore:Int
    var englishScore:Int
    var average:Float
    
   
    init(name:String,mathScore:Int,englishScore:Int) {
        self.name = name
        self.mathScore = mathScore
        self.englishScore = englishScore
        self.average = Float(self.mathScore + self.englishScore) / 2
    }
}
func sortWithAverage(students:[Student]) ->Void {
    var result = students
    for i in 0...result.count-2{
        for j in i+1...result.count-1{
            if(result[i].average > result[j].average){
                let temp = result[i]
                result[i] = result[j]
                result[j] = temp
            }
        }
    }
    for i in 0..<result.count{
        print("姓名:\(result[i].name),数学成绩:\(result[i].mathScore),英语成绩:\(result[i].englishScore),平均分:\(result[i].average)")
    
    }

}


var student01 = Student(name: "st01", mathScore: 67, englishScore: 98)
var student02 = Student(name: "st02", mathScore: 87, englishScore: 56)
var student03 = Student(name: "st03", mathScore: 47, englishScore: 86)
var student04 = Student(name: "st04", mathScore: 65, englishScore: 88)
var student05 = Student(name: "st05", mathScore: 85, englishScore: 97)
var student06 = Student(name: "st06", mathScore: 85, englishScore: 94)
var student07 = Student(name: "st07", mathScore: 92, englishScore: 76)
var student08 = Student(name: "st08", mathScore: 93, englishScore: 66)
var student09 = Student(name: "st09", mathScore: 63, englishScore: 93)
var student10 = Student(name: "st10", mathScore: 99, englishScore: 82)


var result = sortWithAverage(students: [student01,
                                        student02,
                                        student03,
                                        student04,
                                        student05,
                                        student06,
                                        student07,
                                        student08,
                                        student09,
                                        student10])


